home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / moni / Sysmon120a.lha / sysmon / src / BcMTest.c < prev    next >
C/C++ Source or Header  |  2000-05-30  |  4KB  |  160 lines

  1. /*
  2. **    $RCSfile: BcMTest.c,v $
  3. **    $Filename: BcMTest.c $
  4. **    $Revision: 1.1 $
  5. **    $Date: 2000/05/01 15:25:03 $
  6. **
  7. **    sysmon.library Broadcast Message Test Client (version 1.1)
  8. **    
  9. **    (C) Copyright 1996-2000 by Etienne Vogt
  10. */
  11.  
  12. #include <exec/alerts.h>
  13. #include <exec/memory.h>
  14. #include <exec/ports.h>
  15. #include <dos/dos.h>
  16. #include <workbench/startup.h>
  17. #define __USE_SYSBASE
  18. #include <proto/exec.h>
  19. #include <proto/dos.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include "sysmon.h"
  23. #include "sysmon_protos.h"
  24. #include "sysmon_pragmas.h"
  25.  
  26. struct ExecBase *SysBase;
  27. struct DosLibrary *DOSBase;
  28. struct SysmonBase *SysmonBase;
  29. static struct WBStartup *wbmsg;
  30. static struct MsgPort *bcport;
  31. static struct RDArgs *myrda;
  32.  
  33. ULONG __saveds main(void);
  34. static void cleanexit(ULONG rc);
  35. static BOOL SysLog(ULONG priority, APTR format, ...);
  36. static STRPTR bclevel(UBYTE level);
  37.  
  38. static UBYTE version[] = "$VER: BcMTest 1.1 (1.5.2000)";
  39. static UBYTE template[] = "PRI=PRIORITY/K/N,DEBUG/S";
  40.  
  41. #define    OPT_PRIORITY    0
  42. #define    OPT_DEBUG    1
  43. #define    OPTMAX        2
  44.  
  45. ULONG __saveds main(void)    /* No startup code */
  46. {
  47.   struct Process *myproc;
  48.   ULONG signals;
  49.   BOOL abort = FALSE;
  50.   LONG opts[OPTMAX];
  51.  
  52.   SysBase = *(struct ExecBase **)4;
  53.   DOSBase = NULL;
  54.   SysmonBase = NULL;
  55.   wbmsg = NULL;
  56.   bcport = NULL;
  57.   myrda = NULL;
  58.  
  59.   myproc = (struct Process *)FindTask(NULL);
  60.   if ((DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",36)) == NULL)
  61.   { Alert(AT_Recovery|AG_OpenLib|AO_DOSLib);
  62.     return 100;
  63.   }
  64.  
  65.   if (!(myproc->pr_CLI))        /* If started from WB, exit cleanly */
  66.   { WaitPort(&(myproc->pr_MsgPort));
  67.     wbmsg = (struct WBStartup *)GetMsg(&(myproc->pr_MsgPort));
  68.     cleanexit(20);
  69.   }
  70.   else
  71.   {
  72.     if ((SysmonBase = (struct SysmonBase *)OpenLibrary("sysmon.library",1)) == NULL)
  73.     { PutStr("BcMTest: Couldn't open sysmon.library V1\n");
  74.       cleanexit(20);
  75.     }
  76.  
  77.     memset((char *)opts, 0, sizeof(opts));
  78.     if ((myrda = ReadArgs(template, opts, NULL)) == NULL)
  79.     { PrintFault(IoErr(),NULL);
  80.       cleanexit(20);
  81.     }
  82.  
  83.     if ((bcport = CreateMsgPort()) == NULL)
  84.     { PutStr("BcMTest: No memory for struct BroadCastPort\n");
  85.       cleanexit(20);
  86.     }
  87.     bcport->mp_Node.ln_Name = &version[6];
  88.     if (opts[OPT_PRIORITY]) bcport->mp_Node.ln_Pri = *(LONG *)opts[OPT_PRIORITY];
  89.     smAddBroadcastPort(bcport);
  90.  
  91.     while (!abort)
  92.     { signals = Wait(1L << bcport->mp_SigBit | SIGBREAKF_CTRL_C);
  93.  
  94.       if (signals & 1L << bcport->mp_SigBit)
  95.       { struct BroadcastMsg *bcmsg;
  96.  
  97.     while (bcmsg = (struct BroadcastMsg *)GetMsg(bcport))
  98.     { struct BroadcastMsg bcmcopy;
  99.       char msgtxt[256];
  100.  
  101.     /* Copy and reply message so we can't block the sender */
  102.       CopyMem(bcmsg, &bcmcopy, sizeof(struct BroadcastMsg));
  103.       strncpy(msgtxt, bcmsg->bcm_EventTxt, sizeof(msgtxt)-1);
  104.       msgtxt[sizeof(msgtxt)-1] = '\0';
  105.       bcmcopy.bcm_EventTxt = (STRPTR)msgtxt;
  106.       ReplyMsg((struct Message *)bcmsg);
  107.  
  108.       Printf("%s message received from task %08lx\n%s\n",bclevel(bcmcopy.bcm_Level),bcmcopy.bcm_SenderTask,bcmcopy.bcm_EventTxt);
  109.       if (opts[OPT_DEBUG])
  110.       { Printf("bcm_Level        = %ld\n",(LONG)bcmcopy.bcm_Level);
  111.         Printf("bcm_Flags        = %02lx\n",(ULONG)bcmcopy.bcm_Flags);
  112.         Printf("bcm_CountDown    = %ld\n",(ULONG)bcmcopy.bcm_CountDown);
  113.         Printf("bcm_TimeOut      = %ld\n",(ULONG)bcmcopy.bcm_TimeOut);
  114.         Printf("bcm_ReplyCount   = %ld\n",(ULONG)bcmcopy.bcm_ReplyCount);
  115.         Printf("bcm_TimeOutCount = %ld\n",(ULONG)bcmcopy.bcm_TimeOutCount);
  116.       }
  117.     }
  118.       }
  119.       if (signals & SIGBREAKF_CTRL_C) abort = TRUE;
  120.     }
  121.     PutStr("BcMTest exiting\n");
  122.     smRemBroadcastPort(bcport);
  123.   }
  124.   cleanexit(0);
  125. }
  126.  
  127. static void cleanexit(ULONG rc)
  128. {
  129.   if (bcport) DeleteMsgPort(bcport);
  130.   if (myrda) FreeArgs(myrda);
  131.   if (DOSBase) CloseLibrary((struct Library *)DOSBase);
  132.   if (SysmonBase) CloseLibrary((struct Library *)SysmonBase);
  133.   if (wbmsg)
  134.   { Forbid();
  135.     ReplyMsg((struct Message *)wbmsg);
  136.   }
  137.   Exit(rc);
  138. }
  139.  
  140. static BOOL SysLog(ULONG priority, APTR format, ...)
  141. { return smVSysLog(priority, format, &format + 1);
  142. }
  143.  
  144. static STRPTR bclevel(UBYTE level)
  145. {
  146.   switch (level)
  147.   { case BCM_HALT:
  148.     case BCM_UNMOUNT:
  149.     case BCM_SHUTDOWN:
  150.       return "\a\a\aSHUTDOWN";
  151.     case BCM_URGENT:
  152.       return "\a\aURGENT";
  153.     case BCM_NORMAL:
  154.     case BCM_DEBUG:
  155.       return "\aBROADCAST";
  156.     default:
  157.       return "UNKNOWN";
  158.   }
  159. }
  160.